/*
MIT Licence allows free use in all software (including GPL and commercial)

The licence text below is the boilerplate "MIT Licence" used from:
http://www.opensource.org/licenses/mit-license.php

Copyright (c) 2012, An Pham

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "stdafx.h"
#include <windows.h> 
#include <string>
#include <vector>
#include <stdio.h>
#include <sstream>
#include <locale>
#include <algorithm>
#include <functional>
#include "MFLinkToolUtil.h"

using namespace std;

int strCompare(const string& AStr1, const string& AStr2)
{
	return _stricmp(AStr1.c_str(), AStr2.c_str());
}

string& strToUpper(string& AStr)
{
	if (AStr.length())
	    std::transform(AStr.begin(), AStr.end(), AStr.begin(), ::toupper);

    return AStr;
}

wstring& strToUpper(wstring& AStr)
{
	if (AStr.length())
		std::transform(AStr.begin(), AStr.end(), AStr.begin(), ::toupper);

    return AStr;
}

string format(const string& AFmt, ...) 
{
	char Buffer[16384];   
	memset((LPVOID)Buffer, 0, sizeof(Buffer));

	va_list Elements;
    va_start(Elements, AFmt);
	vsnprintf_s(Buffer, sizeof(Buffer) - 1, _TRUNCATE, AFmt.c_str(), Elements);
    va_end(Elements);

	string S(Buffer);
    return S;
}

int indexOf(const vector<string>& AStrs, const string& AStr)
{
	if (0 == AStrs.size())
		return -1;

	int index = 0;
	for each (string S in AStrs)
	{
		if (0 == strCompare(AStr, S))
			return index;
		index++;
	}

	return -1;
}

string valueOf(const vector<string>& AStrs, const string& AName)
{
	if (0 == AStrs.size())
		return "";

	for each (string S in AStrs)
	{
		string Name, Value;
		splitString(S, '=', true, Name, Value);
		if (0 == strCompare(AName, Name))
			return Value;
	}

	return "";
}

int isSpace(int C)
{
	return (isspace(C) | iscntrl(C));
}

string& stringLeftTrim(string& AStr) 
{
	if (AStr.length())
	    AStr.erase(AStr.begin(), find_if(AStr.begin(), AStr.end(), not1(ptr_fun<int, int>(isSpace))));
	return AStr;
}

string& stringRightTrim(string& AStr) 
{
	if (AStr.length())
		AStr.erase(find_if(AStr.rbegin(), AStr.rend(), not1(ptr_fun<int, int>(isSpace))).base(), AStr.end());
	return AStr;
}

string& stringTrim(string& AStr) 
{
	if (0 == AStr.length())
		return AStr;
	else
		return stringLeftTrim(stringRightTrim(AStr));
}

void splitFileName(const string AFullFileName, string& APath, string& AFileName)
{
	APath.clear();
	AFileName.clear();
	if (0 == AFullFileName.length())
		return;
	
	size_t P = AFullFileName.find_last_of(PathSeparator);
	if (string::npos != P)
	{
		APath = AFullFileName.substr(0, P + 1); // Include the last path separator character
		AFileName = AFullFileName.substr(P + 1);
	}
	else
	{
		AFileName = AFullFileName;
	}
}

void splitFileName(const string AFullFileName, string& APath, string& AFileName, string& AExt)
{
	APath.clear();
	AFileName.clear();
	AExt.clear();
	if (0 == AFullFileName.length())
		return;
	
	splitFileName(AFullFileName, APath, AFileName);
	if (0 == AFileName.length())
		return;

	size_t P = AFileName.find_last_of('.');
	if (string::npos != P)
	{
		AExt = AFileName.substr(P + 1); // Exclude dot character
		AFileName = AFileName.substr(0, P);
	}
}

void splitString(const string& AStr, const char ADelimiter, const bool ATrim, string& AFirst, string& ASecond)
{
	AFirst.clear();
	ASecond.clear();
	if (0 == AStr.length())
		return;
	
	size_t P = AStr.find(ADelimiter); 
	if (P != string::npos)
	{
		if (P)
			AFirst.append(AStr.substr(0, P));
		ASecond.append(AStr.substr(P + 1)); 
	}
	else
		AFirst.append(AStr);

	if (ATrim)
	{
		stringTrim(AFirst);
		stringTrim(ASecond);
	}
}

vector<string>& splitString(const string& AStr, const char ADelimiter, vector<string>& AElements) 
{
	if (0 == AStr.length())
		return AElements;
	
	stringstream SS(stringstream::in | stringstream::out);
	SS.write(AStr.c_str(), AStr.length());
	SS.seekg(0, stringstream::beg); 
    string S;
    while (getline(SS, S, ADelimiter))
        AElements.push_back(stringRightTrim(S));
    return AElements;
}


vector<string> splitString(const string& AStr, const char ADelimiter) 
{
    vector<string> Elems;
	if (0 == AStr.length())
		return Elems;
	
    return splitString(AStr, ADelimiter, Elems);
}

vector<string> splitString(const LPVOID AStr, const DWORD ASize, const char ADelimiter)
{
    vector<string> Elems;
	if (0 == ASize)
		return Elems;

	string S((char*)AStr, ASize);
    return splitString(S, ADelimiter, Elems);
}

int64 fileSize(istream* AStream)
{
    AStream->seekg(0, ios::end);
    int64 Size = AStream->tellg();
    AStream->seekg(0, ios::beg);
	return Size;
}

#ifdef LOG
void log(const string AStr)
{
	log(AStr.c_str());
}

void log(const char* AStr)
{
	FILE* File = NULL;
	if (0 == fopen_s(&File, "C:\\Windows\\Temps\\Rdp.log", "a"))
	{
		fprintf(File, "%s\n", AStr);
		fclose(File);		
	}
}
#endif
